/*
 * Copyright (C) 2011 Olivier Pothin and contributors
 *
 * GNU General Public License v2.0
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/**
 * 
 * @author Olivier Pothin
 *
 */
package opot.activities;

import opot.db.WordProvider;
import opot.dto.Word;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class WordManagement extends Activity implements OnClickListener {
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.add_word);
		
		findViewById(R.id.button_add_word).setOnClickListener(this);
		
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}

	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.button_add_word:
		{
			addWord();
		}
			break;
		default:
			break;
		}
	}
	
	private void addWord() {
		EditText text = ( EditText )findViewById( R.id.add_word_text_fr );
		String wordFr = text.getText().toString();
		
		text = ( EditText )findViewById( R.id.add_word_text_pin );
		String wordPin = text.getText().toString();
		
		text = ( EditText )findViewById( R.id.add_word_text_zh );
		String wordZh = text.getText( ).toString( );

		//on vérifie que le mot francais n'existe pas deja
		 Uri frmot = Uri.parse(
	        "content://"+WordProvider.AUTHORITY+"/words/fr/"+wordFr);
	     Cursor c = managedQuery( frmot, null, null, null, "created desc" );
	     if ( c.moveToFirst( ) ) {
	    	 String t =  c.getString( c.getColumnIndex( Word.Words.COLUMN_NAME_FRENCH ) );
			 c.close();
			 Toast.makeText( this, getString( R.string.add_word_deny, t ), Toast.LENGTH_LONG ).show();
	     } else {
	    	 // Pas d'ajout en cas de colonne manquante
	    	 // DO NOT USE isEmpty because it was introduced in API lvl 9
	    	 if(wordPin.trim().length() == 0 || wordZh.trim().length() == 0) {
	    		 Toast.makeText(this, getString(R.string.add_word_missing), Toast.LENGTH_LONG).show();
	    	 } else {
	    		 ContentValues values = new ContentValues();
	    		 values.put(Word.Words.COLUMN_NAME_FRENCH,wordFr);
	    		 values.put(Word.Words.COLUMN_NAME_CHINESE,wordZh);
	    		 values.put(Word.Words.COLUMN_NAME_PINYIN,wordPin);
	    		 getContentResolver().insert(Word.Words.CONTENT_URI,values);
	    	 }
	     }
	}
}
